﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Threading;
using System.Threading.Tasks;


namespace Amarok.Agents
{
	/// <summary>
	/// This type represents the base class for agent implementations. Derived types must implement an agent-specific
	/// constructor that accepts agent environment and agent options. The agent instance is fully operational directly
	/// after construction and can receive and send messages.
	/// 
	/// Inside the agent various overloaded methods can be used to post and publish messages. Posting messages will
	/// send them to the agent instance itself. This can be used to implement workflows or state machines. Publishing 
	/// messages will send them to the outside world, meaning to subscribed agents and/or to a connected message bus, 
	/// which further forwards the messages to connected agents.
	/// </summary>
	public partial class Agent
	{
		#region Publish(Message)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="message">
		/// The message that should be published.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected void Publish(
			Message message)
		{
			mPublisher.Publish(message);
		}

		#endregion

		#region Publish(Object, Message)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="label">
		/// The label that should be assigned to the message before publishing.</param>
		/// <param name="message">
		/// The message that should be published.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected void Publish(
			Object label,
			Message message)
		{
			mPublisher.Publish(label, message);
		}

		#endregion


		#region PublishAfter(TimeSpan, Message)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="delay">
		/// The time span to wait before publishing the message.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. The Task is completed in RanToCompletion state 
		/// once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		protected Task PublishAfter(
			TimeSpan delay,
			Message message)
		{
			return mPublisher.PublishAfter(delay, message);
		}

		#endregion

		#region PublishAfter(Int32, Message)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="millisecondsDelay">
		/// The number of milliseconds to wait before publishing the message.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. The Task is completed in RanToCompletion state 
		/// once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		protected Task PublishAfter(
			Int32 millisecondsDelay,
			Message message)
		{
			return mPublisher.PublishAfter(millisecondsDelay, message);
		}

		#endregion

		#region PublishAfter(TimeSpan, Message, CancellationToken)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="delay">
		/// The time span to wait before publishing the message.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// <param name="cancellationToken">
		/// The cancellation token that can be used to cancel the publishing of the message.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. If the cancellation token is signaled before the 
		/// specified time delay, then the Task is completed in Canceled state. Otherwise, the Task is completed in 
		/// RanToCompletion state once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		/// <exception cref="ObjectDisposedException">
		/// The provided cancellation token has already been disposed.</exception>
		protected Task PublishAfter(
			TimeSpan delay,
			Message message,
			CancellationToken cancellationToken)
		{
			return mPublisher.PublishAfter(delay, message, cancellationToken);
		}

		#endregion

		#region PublishAfter(Int32, Message, CancellationToken)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="millisecondsDelay">
		/// The number of milliseconds to wait before publishing the message.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// <param name="cancellationToken">
		/// The cancellation token that can be used to cancel the publishing of the message.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. If the cancellation token is signaled before the 
		/// specified time delay, then the Task is completed in Canceled state. Otherwise, the Task is completed in 
		/// RanToCompletion state once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		/// <exception cref="ObjectDisposedException">
		/// The provided cancellation token has already been disposed.</exception>
		protected Task PublishAfter(
			Int32 millisecondsDelay,
			Message message,
			CancellationToken cancellationToken)
		{
			return mPublisher.PublishAfter(millisecondsDelay, message, cancellationToken);
		}

		#endregion


		#region PublishAfter(TimeSpan, Object, Message)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="delay">
		/// The time span to wait before publishing the message.</param>
		/// <param name="label">
		/// The label that should be assigned to the message before being dispatched.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. The Task is completed in RanToCompletion state 
		/// once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		protected Task PublishAfter(
			TimeSpan delay,
			Object label,
			Message message)
		{
			return mPublisher.PublishAfter(delay, label, message);
		}

		#endregion

		#region PublishAfter(Int32, Object, Message)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="millisecondsDelay">
		/// The number of milliseconds to wait before publishing the message.</param>
		/// <param name="label">
		/// The label that should be assigned to the message before being dispatched.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. The Task is completed in RanToCompletion state 
		/// once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		protected Task PublishAfter(
			Int32 millisecondsDelay,
			Object label,
			Message message)
		{
			return mPublisher.PublishAfter(millisecondsDelay, label, message);
		}

		#endregion

		#region PublishAfter(TimeSpan, Object, Message, CancellationToken)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="delay">
		/// The time span to wait before publishing the message.</param>
		/// <param name="label">
		/// The label that should be assigned to the message before being dispatched.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// <param name="cancellationToken">
		/// The cancellation token that can be used to cancel the publishing of the message.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. If the cancellation token is signaled before the 
		/// specified time delay, then the Task is completed in Canceled state. Otherwise, the Task is completed in 
		/// RanToCompletion state once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		/// <exception cref="ObjectDisposedException">
		/// The provided cancellation token has already been disposed.</exception>
		protected Task PublishAfter(
			TimeSpan delay,
			Object label,
			Message message,
			CancellationToken cancellationToken)
		{
			return mPublisher.PublishAfter(delay, label, message, cancellationToken);
		}

		#endregion

		#region PublishAfter(Int32, Object, Message, CancellationToken)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers after a given time delay.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <param name="millisecondsDelay">
		/// The number of milliseconds to wait before publishing the message.</param>
		/// <param name="label">
		/// The label that should be assigned to the message before being dispatched.</param>
		/// <param name="message">
		/// The message to be posted to the publisher.</param>
		/// <param name="cancellationToken">
		/// The cancellation token that can be used to cancel the publishing of the message.</param>
		/// 
		/// <returns>
		/// A task that represents whether the message was published. If the cancellation token is signaled before the 
		/// specified time delay, then the Task is completed in Canceled state. Otherwise, the Task is completed in 
		/// RanToCompletion state once the specified time delay has expired and the message has been published.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentOutOfRangeException">
		/// A value was passed to a method that did not accept it as a valid argument, because the value 
		/// must be positive (equal to or greater than zero).</exception>
		/// <exception cref="ObjectDisposedException">
		/// The provided cancellation token has already been disposed.</exception>
		protected Task PublishAfter(
			Int32 millisecondsDelay,
			Object label,
			Message message,
			CancellationToken cancellationToken)
		{
			return mPublisher.PublishAfter(millisecondsDelay, label, message, cancellationToken);
		}

		#endregion


		#region PublishOperation(Operation)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <typeparam name="TResult">
		/// The type of result value that is returned from the operation.</typeparam>
		/// <typeparam name="TProgress">
		/// The type of progress value that is used to report progress while the operation is running.</typeparam>
		/// 
		/// <param name="operation">
		/// The operation message that should be published.</param>
		/// <param name="progressAction">
		/// An optional callback that is called to handle progress values reported from the operation.</param>
		/// 
		/// <returns>
		/// A task that represents the asynchronously running operation. This task can be used to wait for completion, 
		/// to register a continuation and to access state and result of the operation.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected Task<TResult> PublishOperation<TResult, TProgress>(
			Operation<TResult, TProgress> operation,
			Action<TProgress> progressAction = null)
		{
			return mPublisher.PublishOperation(operation, progressAction);
		}

		#endregion

		#region PublishOperation(Operation, CancellationToken)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <typeparam name="TResult">
		/// The type of result value that is returned from the operation.</typeparam>
		/// <typeparam name="TProgress">
		/// The type of progress value that is used to report progress while the operation is running.</typeparam>
		/// 
		/// <param name="operation">
		/// The operation message that should be published.</param>
		/// <param name="cancellationToken">
		/// An CancellationToken that can be used to cancel the operation.</param>
		/// <param name="progressAction">
		/// An optional callback that is called to handle progress values reported from the operation.</param>
		/// 
		/// <returns>
		/// A task that represents the asynchronously running operation. This task can be used to wait for completion, 
		/// to register a continuation and to access state and result of the operation.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected Task<TResult> PublishOperation<TResult, TProgress>(
			Operation<TResult, TProgress> operation,
			CancellationToken cancellationToken,
			Action<TProgress> progressAction = null)
		{
			return mPublisher.PublishOperation(operation, cancellationToken, progressAction);
		}

		#endregion


		#region PublishOperation(Object, Operation)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <typeparam name="TResult">
		/// The type of result value that is returned from the operation.</typeparam>
		/// <typeparam name="TProgress">
		/// The type of progress value that is used to report progress while the operation is running.</typeparam>
		/// 
		/// <param name="label">
		/// The label that should be assigned to the message before publishing.</param>
		/// <param name="operation">
		/// The operation message that should be published.</param>
		/// <param name="progressAction">
		/// An optional callback that is called to handle progress values reported from the operation.</param>
		/// 
		/// <returns>
		/// A task that represents the asynchronously running operation. This task can be used to wait for completion, 
		/// to register a continuation and to access state and result of the operation.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected Task<TResult> PublishOperation<TResult, TProgress>(
			Object label,
			Operation<TResult, TProgress> operation,
			Action<TProgress> progressAction = null)
		{
			return mPublisher.PublishOperation(label, operation, progressAction);
		}

		#endregion

		#region PublishOperation(Object, Operation, CancellationToken)

		/// <summary>
		/// Publishes the supplied message and forwards it to interested consumers.
		/// 
		/// The method stores the supplied message in the dispatcher's input message queues of interested consumers and 
		/// returns immediately, meaning it only schedules the message for further processing, but the actual processing 
		/// is done later in other threads.
		/// 
		/// If there is no interested consumer at the time of publishing, then the message will be dropped silently.
		/// 
		/// Messages that are published after completing the publisher will be dropped silently.
		/// </summary>
		/// 
		/// <typeparam name="TResult">
		/// The type of result value that is returned from the operation.</typeparam>
		/// <typeparam name="TProgress">
		/// The type of progress value that is used to report progress while the operation is running.</typeparam>
		/// 
		/// <param name="label">
		/// The label that should be assigned to the message before publishing.</param>
		/// <param name="operation">
		/// The operation message that should be published.</param>
		/// <param name="cancellationToken">
		/// An CancellationToken that can be used to cancel the operation.</param>
		/// <param name="progressAction">
		/// An optional callback that is called to handle progress values reported from the operation.</param>
		/// 
		/// <returns>
		/// A task that represents the asynchronously running operation. This task can be used to wait for completion, 
		/// to register a continuation and to access state and result of the operation.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		protected Task<TResult> PublishOperation<TResult, TProgress>(
			Object label,
			Operation<TResult, TProgress> operation,
			CancellationToken cancellationToken,
			Action<TProgress> progressAction = null)
		{
			return mPublisher.PublishOperation(label, operation, cancellationToken, progressAction);
		}

		#endregion

	}
}
